<template>

  <div>
    <div class="filter-container">
      <div class="filter-item">
        <span>模版名称:</span>
        <el-input v-model="listQuery.name" clearable placeholder="请输入模版名称" @keyup.enter="handleFilter" />
      </div>
      <div class="filter-item">
        <span>模版编码:</span>
        <el-input v-model="listQuery.code" clearable placeholder="请输入模版编码" @keyup.enter="handleFilter" />
      </div>
      <el-button class="filter-item" type="primary" icon="Search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" text type="primary" icon="Delete" @click="clearQuery">清除条件</el-button>

    </div>
    <div class="operationList">
      <div>
        <el-button text type="primary" icon="Plus" @click="handleCreate">新增</el-button>
      </div>
      <div>
        <el-button text type="primary" icon="RefreshRight" @click="handleFilter">刷新</el-button>
      </div>
    </div>
    <el-table v-loading="listLoading" :row-class-name="tableRowClass" :data="list" fit border highlight-current-row style="width: 100%" header-cell-class-name="header-cell"
    >
      <el-table-column label="序号" width="100" type="index" :index="calIndex"/>
      <el-table-column label="模版名称">
        <template #default="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="180" label="模版编码">
        <template #default="scope">
          <span>{{ scope.row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button v-permission="['email:template:update']" size="small" plain type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['email:template:delete']" size="small" plain type="danger" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" v-model:page="listQuery.page" v-model:limit="listQuery.limit" :total="total" :page-sizes="pageArray" @pagination="getList" />

    <templateForm ref="templateForm" @refresh-list="getList"/>
  </div>
</template>
<script>
import { fetchTemplateList, deleteTemplate } from '@/api/email/template'
import { getDictList } from '@/utils/dict'
import templateForm from './templateForm.vue'
import Pagination from '@/components/Pagination/index.vue'
export default {
  name: 'TemplateList',
  components: { Pagination, templateForm },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      pageArray: this.$store.dictStore.pageArray,
      listQuery: {
        page: 1,
        limit: this.$store.dictStore.defaultPageSize,
        importance: undefined,
        title: undefined,
        type: undefined
      },
      businessTypeOptions: getDictList('business_type'),
      showReviewer: false,
      temp: {
        id: undefined,
        name: '',
        code: '',
        businessType: '',
        templateSubject: '',
        templateContent: '',
        configId: '',
        appId: ''

      },
      loading: false,
      dialogPvVisible: false,
      pvData: [],
      appList: null,
      configList: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 获取列表
     */
    getList(loading = true) {
      this.listLoading = loading
      fetchTemplateList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    /**
     * 搜索列表
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    /**
     * 修改状态
     * @param row 当前列表选中数据
     * @param status 状态
     */
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },

    /**
     * 新增
     */
    handleCreate() {
      this.$refs.templateForm.handleCreate()
    },

    /**
     * 编辑
     * @param row 当前列表选中数据
     */
    handleUpdate(row) {
      this.$refs.templateForm.handleUpdate(row)
    },

    /**
     * 删除
     * @param row 当前列表选中数据
     */
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTemplate([row.id]).then(response => {
          if (response.data.success) {
            row.$delAnim = true
            this.getList(false)
            this.$message.success(response.data.msg)
          } else {
            this.getList(false)
            this.$message.error(response.data.msg)
          }
        })
      })
    }
  }
}
</script>
